# -*- coding:utf-8 -*-
import requests as nq

from kivy.config import Config 

Config.set('kivy','default_font', ["Arial", "./ap.ttf"]) 

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.clock import Clock

nq.packages.urllib3.disable_warnings()
netAdress = 'develop.api.ct10649.com'
port = '8743'
cert = ('./client.crt', './client.key')
appID = 'Y8W_1ZxbNak8_HSQgRkHIbt2_jka'
secret = 'YCCuEKEGY3tbh7LCpoKiEjTJtHAa'
deviceID = gateWayId = 'eaec8bb4-51f4-4362-966a-ac16f6e27deb'
accessToken = ''


def conectIoT(appID, secret, cert, netAdress='develop.api.ct10649.com', port='8743'):
    url = 'https://' + netAdress + ':' + port + '/iocm/app/sec/v1.1.0/login'
    r = nq.post(url, data={'appId': appID, 'secret': secret}, cert=cert, verify=False)
    if r.status_code == nq.codes.ok:
        d = eval(r.text)
        if d['accessToken']:
            return d
        else:
            return

def getDeviceMsg(appID, accessToken, deviceID, cert, netAdress='develop.api.ct10649.com', port='8743'):
    url = "https://" + netAdress + ":" + port + "/iocm/app/dm/v1.4.0/devices/" + deviceID + "?" + "appId=" + appID
    itemHeaders = {"app_key": appID, "Authorization": "Bearer " + accessToken, "Content-Type": "applicaton/json"}
    r = nq.get(url, headers=itemHeaders, cert=cert, verify=False)
    # data = eval(r.text)
    a,b=r.text.split(':[')
    
    return b.replace(']}','')

class LoginScreen(GridLayout):
    def __init__(self,**kwargs):
        super(LoginScreen,self).__init__(**kwargs)
        
        self.step=0
        self.first_flag=0

        self.cols=2

        self.add_widget(Label(text='浊度：',font_size=50))
        self.tu=Label(text='--',font_size=50)
        self.add_widget(self.tu)

        self.add_widget(Label(text='温度：',font_size=50))
        self.tp=Label(text='--',font_size=50)
        self.add_widget(self.tp)

        self.add_widget(Label(text='湿度：',font_size=50))
        self.sd=Label(text='--',font_size=50)
        self.add_widget(self.sd)

        self.add_widget(Label(text='PH值：',font_size=50))
        self.ph=Label(text='--',font_size=50)
        self.add_widget(self.ph)
        
        self.add_widget(Label(text='水面高度：',font_size=50))
        self.h=Label(text='--',font_size=50)
        self.add_widget(self.h)

        self.add_widget(Label(text='浊度状态：',font_size=50))
        self.w_tu=Label(text='--',font_size=50)
        self.add_widget(self.w_tu)

        self.add_widget(Label(text='P H 状态：',font_size=50))
        self.w_ph=Label(text='--',font_size=50)
        self.add_widget(self.w_ph)

        self.add_widget(Label(text='水面状态：',font_size=50))
        self.w_h=Label(text='--',font_size=50)
        self.add_widget(self.w_h)

        fm=Button(text='已排除异物',font_size=50)
        self.add_widget(fm)
        fm.bind(on_press=self.callback)
        Clock.schedule_interval(self.clock_event, 5)

    def callback(self,event):
        self.w_h.text='正常'
        self.w_h.color=(0,1,0,1)

    def clock_event(self,n):
        accessToken = (dict(conectIoT(appID,secret,cert)))['accessToken']
        data = (getDeviceMsg(appID,accessToken,deviceID,cert))[66:103]
        #print(data)
        fm=nq.get('http://www.svip886.cn/1.txt').text
        tu,tp,sd,h,ph=data.replace('TU:','').replace('TP:','').replace('PH:','').replace('H:','').replace('C','').replace('mm','').split()
        
        self.tu.text=tu
        self.tp.text=tp+'℃'
        self.sd.text=sd
        self.h.text=h+'mm'
        self.ph.text=ph
        self.fm=fm
        if float(tu)>200:
            self.w_tu.text='异常'
            self.w_tu.color=(1,0,0,1)
        else:
            self.w_tu.text='正常'
            self.w_tu.color=(0,1,0,1)
        if float(ph)>=8.5 or float(ph)<5.5:
            self.w_ph.text='异常'
            self.w_ph.color=(1,0,0,1)
        else:
            self.w_ph.text='正常'
            self.w_ph.color=(0,1,0,1)
        if self.fm=='1':
            self.w_h.text='异物入水'  
            self.w_h.color=(1,0,0,1)
        else:
            self.w_h.text='正常'
            self.w_h.color=(0,1,0,1)
        if self.step!=int(h):
            self.first_flag=1

        self.step=int(h)

class MyApp(App):
    def build(self):
        return LoginScreen()

if __name__=='__main__':
    MyApp().run()
